package com.ruoyi.system.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Patient;
import com.ruoyi.system.service.IPatientService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 患者Controller
 *
 * @author yangxp
 * @date 2020-08-03
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/medicine/patient")
public class PatientController extends BaseController {

	private final IPatientService iPatientService;

	/**
	 * 查询患者列表
	 */
	@PreAuthorize("@ss.hasPermi('medicine:patient:list')")
	@GetMapping("/list")
	public TableDataInfo list(Patient patient) {
		startPage();
		LambdaQueryWrapper<Patient> lqw = new LambdaQueryWrapper<Patient>();
		if (StringUtils.isNotBlank(patient.getPatName())) {
			lqw.like(Patient::getPatName, patient.getPatName());
		}
		if (patient.getPatGender() != null) {
			lqw.eq(Patient::getPatGender, patient.getPatGender());
		}
		if (StringUtils.isNotBlank(patient.getPatPhone())) {
			lqw.like(Patient::getPatPhone, patient.getPatPhone());
		}
		if (StringUtils.isNotBlank(patient.getPatId())) {
            lqw.eq(Patient::getPatId,patient.getPatId());
		}
		List<Patient> list = iPatientService.list(lqw);
		return getDataTable(list);
	}

	/**
	 * 导出患者列表
	 */
	@PreAuthorize("@ss.hasPermi('medicine:patient:export')")
	@Log(title = "患者", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(Patient patient) {
		LambdaQueryWrapper<Patient> lqw = new LambdaQueryWrapper<Patient>(patient);
		List<Patient> list = iPatientService.list(lqw);
		ExcelUtil<Patient> util = new ExcelUtil<Patient>(Patient.class);
		return util.exportExcel(list, "patient");
	}

	/**
	 * 获取患者详细信息
	 */
	@PreAuthorize("@ss.hasPermi('medicine:patient:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(iPatientService.getById(id));
	}

	/**
	 * 新增患者
	 */
	@PreAuthorize("@ss.hasPermi('medicine:patient:add')")
	@Log(title = "患者", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody Patient patient) {

		patient.setPatId(IdUtil.fastSimpleUUID());
		patient.setGmtCreate(DateUtil.date());
		patient.setGmyModified(DateUtil.date());
		return toAjax(iPatientService.save(patient) ? 1 : 0);
	}

	/**
	 * 修改患者
	 */
	@PreAuthorize("@ss.hasPermi('medicine:patient:edit')")
	@Log(title = "患者", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody Patient patient) {
		patient.setGmyModified(DateUtil.date());
		return toAjax(iPatientService.updateById(patient) ? 1 : 0);
	}

	/**
	 * 删除患者
	 */
	@PreAuthorize("@ss.hasPermi('medicine:patient:remove')")
	@Log(title = "患者", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(iPatientService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
	}
}
